package com.example.propertymanagement.controller;

import com.example.propertymanagement.dto.VehicleDTO;
import com.example.propertymanagement.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * VehicleController：车辆信息的控制层，负责接收前端请求并返回响应
 */
@RestController
@RequestMapping("/api/vehicles")
public class VehicleController {

    @Autowired
    private VehicleService vehicleService;

    /**
     * 获取所有车辆信息
     * @return ResponseEntity<List<VehicleDTO>> 车辆信息列表
     */
    @GetMapping
    public ResponseEntity<List<VehicleDTO>> getAllVehicles() {
        List<VehicleDTO> vehicles = vehicleService.getAllVehicles();
        return ResponseEntity.ok(vehicles);
    }

    /**
     * 根据车主ID获取车辆信息
     * @param ownerId 车主ID
     * @return ResponseEntity<List<VehicleDTO>> 车辆信息列表
     */
    @GetMapping("/owner/{ownerId}")
    public ResponseEntity<List<VehicleDTO>> getVehiclesByOwnerId(@PathVariable Integer ownerId) {
        List<VehicleDTO> vehicles = vehicleService.getVehiclesByOwnerId(ownerId);
        return ResponseEntity.ok(vehicles);
    }

    /**
     * 根据车牌号查找车辆
     * @param plateNumber 车牌号
     * @return ResponseEntity<VehicleDTO> 车辆信息
     */
    @GetMapping("/plate/{plateNumber}")
    public ResponseEntity<VehicleDTO> getVehicleByPlateNumber(@PathVariable String plateNumber) {
        VehicleDTO vehicle = vehicleService.getVehicleByPlateNumber(plateNumber);
        return vehicle != null ? ResponseEntity.ok(vehicle) : ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
    }

    /**
     * 添加新车辆信息
     * @param vehicleDTO 车辆信息
     * @return ResponseEntity<VehicleDTO> 新添加的车辆信息
     */
    @PostMapping
    public ResponseEntity<VehicleDTO> addVehicle(@RequestBody VehicleDTO vehicleDTO) {
        VehicleDTO createdVehicle = vehicleService.addVehicle(vehicleDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdVehicle);
    }

    /**
     * 删除车辆
     * @param vehicleId 车辆ID
     * @return ResponseEntity<Void> 响应结果
     */
    @DeleteMapping("/{vehicleId}")
    public ResponseEntity<Void> deleteVehicle(@PathVariable Integer vehicleId) {
        vehicleService.deleteVehicle(vehicleId);
        return ResponseEntity.noContent().build();
    }

    /**
     * 更新车辆信息
     * @param vehicleId 车辆ID
     * @param vehicleDTO 更新后的车辆信息
     * @return ResponseEntity<VehicleDTO> 更新后的车辆信息
     */
    @PutMapping("/{vehicleId}")
    public ResponseEntity<VehicleDTO> updateVehicle(@PathVariable Integer vehicleId, @RequestBody VehicleDTO vehicleDTO) {
        // 确保传入的 vehicleDTO 包含 vehicleId
        vehicleDTO.setId(vehicleId);  // 将路径参数中的 vehicleId 设置到 DTO 中

        VehicleDTO updatedVehicle = vehicleService.updateVehicle(vehicleDTO);
        return updatedVehicle != null ? ResponseEntity.ok(updatedVehicle) : ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
    }
}